package com.ff.xyh.consumer.online.user.listener;

import com.alibaba.fastjson.JSON;
import com.ff.xyh.common.utils.CommonUtil;
import com.ff.xyh.common.vo.message.ActionTypeAndRoomMessage;
import com.ff.xyh.common.vo.message.RoomMessageVo;
import com.ff.xyh.consumer.online.user.config.MyLock;
import com.ff.xyh.consumer.online.user.config.UserRabbitConfig;
import com.ff.xyh.consumer.online.user.config.Users;
import com.ff.xyh.consumer.online.user.controller.OnlineUserController;
import com.ff.xyh.consumer.online.user.handler.UserActionHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @Auther FF
 * @Date 2022年 11月 30日
 * @Time 00:38
 */
@Component
public class UserRabbitListener {

    @Autowired
    private UserActionHandler handler;

    /** 消息处理队列 */
    @RabbitListener(queues = UserRabbitConfig.QUEUE_SERVER_MESSAGE)
    public void messageQueue1Consumer1(ActionTypeAndRoomMessage message) throws InterruptedException {

        System.out.println("messageQueue1Consumer1 \n " +
                "queue: " + UserRabbitConfig.QUEUE_SERVER_MESSAGE + " \n " +
                "routingKey: " + UserRabbitConfig.ROUTING_KEY_MESSAGE + " \n" +
                "controller with: { " + message + " }, when " + LocalTime.now());
//        获取消息体中的数据
        int actionType = message.getActionType();
        switch (actionType) {
            case 0:
                handler.unknownAction(message);
                break;
            case 1:
                handler.commentAction(message);
                break;
            case 2:
                handler.collectAction(message);
                break;
            case 3:
                handler.clickLikeAction(message);
                break;
            case 4:
                handler.followAction(message);
                break;
            case 5:
                handler.inRoomAction(message);
                break;
//            case 6:
//
//                break;
//            case 7:
//
//                break;
            case 8:
                handler.outRoomAction(message);
                break;
            case 9:
                handler.sendMessage(message);
                break;
//            case 10:
//
//                break;
//            case 11:
//
//                break;
            case 12:
                handler.kickRoomUser(message);
                break;
//            case 13:
//
//                break;
//            case 14:
//
//                break;
            case 15:
                handler.beingKicked(message);
                break;
            case 16:
                handler.beingLogin(message);
                break;
            case 17:
                handler.loginOff(message);
                break;
            case 18:
                handler.loginSuccess(message);
                break;
//            case 19:
//
//                break;
            default:
                handler.unknownAction(message);
                break;
        }
    }

    // 消息处理队列
    @RabbitListener(queues = UserRabbitConfig.QUEUE_SERVER_USER)
    public void userQueue1Consumer1(ActionTypeAndRoomMessage message) {
        System.out.println("userQueue1Consumer1 got message: " + message);
        RoomMessageVo roomMessageVo = CommonUtil.getMessageInMap(message.getMessageBody(), RoomMessageVo.class);
        String userId = roomMessageVo.getToUserId();
        if (StringUtils.isEmpty(userId)) {
            userId = roomMessageVo.getUserId();
        }
        int actionType = message.getActionType();
        OnlineUserController onlineUserController = Users.getUsers().get(userId);
        if (onlineUserController != null) {
            Session session = onlineUserController.getSession();
//            session.getAsyncRemote().sendObject(message); // 发送封装的对象
            session.getAsyncRemote().sendText(JSON.toJSONString(message)); // 发送字符串
            try {
                if (
//                        actionType == 0 ||
//                                actionType == 7 ||
//                                actionType == 15 ||
                                actionType == 16 ||
                                actionType == 19 ) {
                    session.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("用户已断开连接");
        }
    }

    // 用户登录锁消息队列
    @RabbitListener(queues = UserRabbitConfig.QUEUE_SERVER_USER_LOCK)
    public void userLockQueue1Consumer1(ActionTypeAndRoomMessage actionTypeAndRoomMessage) {
        System.out.println("userLockQueue1Consumer1 got message: " + actionTypeAndRoomMessage);
        RoomMessageVo roomMessageVo = CommonUtil.getMessageInMap(actionTypeAndRoomMessage.getMessageBody(), RoomMessageVo.class);
        String userId = roomMessageVo.getUserId();
        MyLock lock = MyLock.getLocks().get(userId);
        if (lock != null) {
            lock.confirmed();
        } else {
            System.out.println("RoomUserController.UserLock is null");
        }
        System.out.println("ROUTING_KEY_USER_LOCK_1 has been notified when " + LocalDateTime.now());
    }
}
